<template>
  <h2>使用纯类型标注来声明触发的事件</h2>
  <div class="note">
    <p>1. 使用泛型定义defineEmits事件，可以定义多个事件类型（约束）</p>
  </div>

  <hr class="hr-style" />
  <div class="test">
    <div>{{  tle  }}</div>
    <button type="button" @click="editTitle">
      emit触发父组件绑定子组件上的函数2(修改title的值)
    </button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const emit = defineEmits<{
  (e: 'change', id: number): void
  (e: 'update', value: string): void
  (e: 'edit', val1: string, val2: number): void
  (e: 'editUser', user: Iuser): number
}>()

export interface Iuser {
  name: string
  sex: string
  age: number
}

const {
  title = '江山如此多娇,引无数英雄竞折腰',
  msg,
  user
} = defineProps<{
  title?: string
  user?: Iuser
  msg?: string
}>()

const tle = ref(title)

const editTitle = () => {
  tle.value = '数风流人物,还看今朝'
  emit('editUser', { age: 2, name: '毛泽东', sex: '男神' })
}
</script>

<style scoped>
body {
  padding: 20px;
}
h2 {
  color: red;
}
h3 {
  color: rgb(220, 85, 85);
}
h4 {
  color: skyblue;
}
.hr-style {
  border: 3px solid gold;
  margin: 20px 0;
}
.note {
  margin: 10px;
  padding: 10px;
  border: 3px solid rgb(221, 121, 202);
}
.note p {
  color: rgb(226, 106, 204);
  font-weight: bold;
}
</style>
